package com.bidianying.dao;

import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.jdbc.core.BatchPreparedStatementSetter;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.support.rowset.SqlRowSet;
import org.springframework.stereotype.Repository;

import com.bidianying.entity.City;

@Repository
public class GewaraDao {
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
	public int saveCityIdForGewara(List<City> listCity){
		final List<City> tempList = listCity;   
		final SimpleDateFormat sdformat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String sql = "insert into gewara_city(city_id,city_name,city_code,city_pinyin,update_time) values(?,?,?,?,?)";
		
		int[] resultInt = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter(){
			@Override
			public void setValues(PreparedStatement ps, int i)throws SQLException {
				ps.setString(1, tempList.get(i).getCityId());
				ps.setString(2, tempList.get(i).getCityName());
				ps.setString(3,tempList.get(i).getCityCode());
				ps.setString(4,tempList.get(i).getCityPinyin());
				ps.setString(5,sdformat.format(new Date()));
			}
			@Override
			public int getBatchSize() {
				// TODO Auto-generated method stub
				return tempList.size();
			}
		});
		return resultInt.length;
	}
	
	public JSONArray getCity(){
		JSONArray jsonArray = new JSONArray();
		String sql = "select * from gewara_city";
		SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);
		while(rs.next()){
			JSONObject jsonObject = new JSONObject();
			jsonObject.put("name", rs.getString("city_name"));
			jsonObject.put("pinyin", rs.getString("city_pinyin"));
			jsonObject.put("code", rs.getString("city_code"));
			jsonArray.add(jsonObject);
		}
		return jsonArray;
	}
	
	public Map<String, Object> getCityNameById(String citycode){
		String sql = "select * from gewara_city where city_code=?";
		Map<String, Object> map = jdbcTemplate.queryForMap(sql, citycode);
		return map;
	}
	
	public int saveMovieInfoForGewara(List<Map<String,String>> list){
		final List<Map<String,String>> tempList = list;   
		String sql = "insert into tMovie_info(gewaraMovieId,movieName) values(?,?)";
		int[] resultInt = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter(){
			@Override
			public void setValues(PreparedStatement ps, int i)throws SQLException {
				ps.setString(1, tempList.get(i).get("movieid"));
				ps.setString(2, tempList.get(i).get("title"));
			}
			@Override
			public int getBatchSize() {
				return tempList.size();
			}
		});
		return resultInt.length;
	}
	
	public Set<String> getAllGewaraMovieId(){
		Set<String> resultSet = new HashSet<String>();
		String sql = "select * from tMovie_info";
		SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);
		while(rs.next()){
			resultSet.add(rs.getString("gewaraMovieId"));
		}
		return resultSet;
	}
	
	public Map<String,String> getAllGewaraMovieInfo(){
		Map<String,String> resultMap = new HashMap<String,String>();
		String sql = "select * from tMovie_info";
		SqlRowSet rs = jdbcTemplate.queryForRowSet(sql);
		while(rs.next()){
			resultMap.put(rs.getString("movieName"), rs.getString("id"));
		}
		return resultMap;
	}
	
	public int saveGewaraCinemaInfo(List<Map<String,String>> list){
		final List<Map<String,String>> tempList = list;   
		String sql = "insert into tGewara_cinema(title,cityPinyin,regionName,address,cinemaCode) values(?,?,?,?,?)";
		int[] resultInt = jdbcTemplate.batchUpdate(sql, new BatchPreparedStatementSetter(){
			@Override
			public void setValues(PreparedStatement ps, int i)throws SQLException {
				ps.setString(1, tempList.get(i).get("title"));
				ps.setString(2, tempList.get(i).get("cityPinyin"));
				ps.setString(3, tempList.get(i).get("regionName"));
				ps.setString(4, tempList.get(i).get("address"));
				ps.setString(5, tempList.get(i).get("cinemaCode"));
			}
			@Override
			public int getBatchSize() {
				return tempList.size();
			}
		});
		return resultInt.length;
	}
	
}











